package com.chen.script.job;

import cn.hutool.core.date.DateUtil;
import com.chen.base.utils.BashUtil;
import com.chen.base.utils.file.IoFileUtil;
import com.chen.script.annotation.Job;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.io.File;

/**
 * @Author: 陈锦龙
 * @Date: 2025/3/20 11:09
 */
@Data
@Slf4j
@Component
@ConfigurationProperties(prefix = "oracle-back")
@ConditionalOnProperty(value = "oracle-back.enable", havingValue = "true")
public class OracleBackJob extends AbstractJob{

    public Integer days;
    public String batFilePath;
    public String dmpFilePath;
    public String bakFilePath;

    public OracleBackJob(){
        log.info("初始化 oracle数据备份 JOB");
    }

    @Job("oracle-back")
    public void run() throws Exception {
        // bat 文件
        File[] batFiles = new File(batFilePath).listFiles();
        if (batFiles != null){
            for (File batFile : batFiles) {
                if (batFile.getName().contains(".bat")){
                    log.info("备份: {}", batFile.getName());
                    BashUtil.execBatScript(batFile.getPath());
                }
            }
        }

        // dmp 文件
        String targetPath = bakFilePath + "//" + DateUtil.date().toDateStr();
        log.info("移动DMP文件, {} -> {}", dmpFilePath, targetPath);
        File[] dmpFiles = new File(dmpFilePath).listFiles();
        if (dmpFiles != null){
            for (File dmpFile : dmpFiles) {
                if (dmpFile.isFile()){
                    IoFileUtil.shearFile(dmpFile, targetPath, dmpFile.getName());
                }
            }
        }

        // 删除多余的备份数据，如果为-1，则不做删除
        delBackFileData(days, bakFilePath);
    }

}
